﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TagsViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the AccountViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Windows;
    using System.Windows.Forms;
    using Business.Interfaces;
    using Common;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="TagsViewModel"/> type.
    /// </summary>
    public class TagsViewModel : ViewModelBase
    {
        /// <summary>
        /// The Save Command
        /// </summary>
        public static readonly ActiveAwareCommand EditCommand = new ActiveAwareCommand();

        /// <summary>
        /// The Delete Command
        /// </summary>
        public static readonly ActiveAwareCommand DeleteCommand = new ActiveAwareCommand();

        /// <summary>
        /// The refresh command
        /// </summary>
        public static readonly ActiveAwareCommand RefreshCommand = new ActiveAwareCommand();

        /// <summary>
        /// Dependency Property TagSummary view model
        /// </summary>
        public static readonly DependencyProperty TagSummaryViewModelProperty = DependencyProperty.Register(
            "TagSummaryViewModel",
            typeof(TagSummaryViewModel),
            typeof(TagsViewModel));

        /// <summary>
        /// Account object
        /// </summary>
        private readonly ITagManager _tagManager;

        /// <summary>
        /// The session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Selected Tag
        /// </summary>
        private Tag _selectedTag;

        /// <summary>
        /// Tags observable collection
        /// </summary>
        private ObservableCollection<Tag> _tags;

        /// <summary>
        /// Initializes a new instance of the <see cref="TagsViewModel"/> class.
        /// </summary>
        public TagsViewModel()
            : this(ServiceLocator.Current.GetInstance<ITagManager>(), ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TagsViewModel"/> class.
        /// </summary>
        /// <param name="tagManager">The tag manager.</param>
        /// <param name="session">The session</param>
        public TagsViewModel(ITagManager tagManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(tagManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _tagManager = tagManager;
            _session = session;

            TagSummaryViewModel = new TagSummaryViewModel(this);
        }

        /// <summary>
        /// Gets the accounts.
        /// </summary>
        public ObservableCollection<Tag> Tags
        {
            get
            {
                if (_tags == null)
                {
                    _tags = new ObservableCollection<Tag>(
                        _tagManager
                        .List(new Business.Entities.Tag { UserId = _session.LoggedUser })
                        .Select(x => x.Map<Business.Entities.Tag, Tag>()));
                }

                return _tags;
            }

            private set
            {
                if (value != _tags)
                {
                    _tags = value;

                    OnPropertyChanged("Tags");
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected tag.
        /// </summary>
        /// <value>
        /// The selected tag.
        /// </value>
        public Tag SelectedTag
        {
            get
            {
                return _selectedTag;
            }

            set
            {
                if (_selectedTag != value)
                {
                    _selectedTag = value;
                    OnPropertyChanged("SelectedTag");
                }
            }
        }

        /// <summary>
        /// Gets or sets the tag summary view model.
        /// </summary>
        /// <value>
        /// The tag summary view model.
        /// </value>
        public TagSummaryViewModel TagSummaryViewModel
        {
            get { return (TagSummaryViewModel)GetValue(TagSummaryViewModelProperty); }
            set { SetValue(TagSummaryViewModelProperty, value); }
        }

        /// <summary>
        /// Refreshes the list.
        /// </summary>
        /// <param name="obj">The object</param>
        public void RefreshList(object obj)
        {
            Tags = new ObservableCollection<Tag>(_tagManager.List(new Business.Entities.Tag { UserId = _session.LoggedUser })
                .OrderBy(x => x.Name)
                .Select(x => x.Map<Business.Entities.Tag, Tag>()));
        }

        /// <summary>
        /// Edits the tag.
        /// </summary>
        /// <param name="obj">The object</param>
        public void EditTag(object obj)
        {
            var editorViewModel = new TagEditorViewModel(SelectedTag);
            ServiceLocator.Current.GetInstance<IWindowDialogService>().ShowDialog("Edit Tag", editorViewModel);
        }

        /// <summary>
        /// Called when new tag.
        /// </summary>
        /// <param name="obj">The object</param>
        public void SaveTag(object obj)
        {
            _tagManager.Update(SelectedTag.Map<Tag, Business.Entities.Tag>());
        }

        /// <summary>
        /// Deletes the tag.
        /// </summary>
        /// <param name="obj">The object</param>
        public void DeleteTag(object obj)
        {
            if (ServiceLocator.Current.GetInstance<IWindowDialogService>().ShowMessage(
                   "Are you sure you want to delete selected account?",
                   "Hissab",
                   MessageBoxButtons.YesNo,
                   MessageBoxIcon.Question)
                   == DialogResult.Yes)
            {
                _tagManager.Delete(SelectedTag.Map<Tag, Business.Entities.Tag>());
                Tags.Remove(SelectedTag);
            }
        }

        /// <summary>
        /// Determines whether this instance [can edit tag].
        /// </summary>
        /// <param name="obj">The object</param>
        /// <returns>
        ///   <c>true</c> if this instance [can edit tag]; otherwise, <c>false</c>.
        /// </returns>
        public bool CanEditTag(object obj)
        {
            return SelectedTag != null;
        }

        /// <summary>
        /// Determines whether this instance [can delete tag] the specified obj.
        /// </summary>
        /// <param name="obj">The object</param>
        /// <returns>
        ///   <c>true</c> if this instance [can delete tag] the specified obj; otherwise, <c>false</c>.
        /// </returns>
        public bool CanDeleteTag(object obj)
        {
            return SelectedTag != null;
        }
    }
}